package com.shushan.accessibility.coloros;


import android.os.Build;

import com.shushan.accessibility.AccessibilityOperator;

/**
 * @ClassName: ColorOSOperator
 * @Desciption: oppo辅助功能操作类
 * @author: yichaohua
 * @date: 2018-07-10
 */
public class ColorOSOperator {

    private static final String TAG = "ColorOSOperator";

    private static ColorOSOperator mInstance = new ColorOSOperator();

    public static ColorOSOperator getInstance() {
        return mInstance;
    }

    public boolean closeSavePower() {
        AccessibilityOperator.getInstance().getRecycleView();
        boolean result = AccessibilityOperator.getInstance().clickListItem("android:id/list","耗电保护",1);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            AccessibilityOperator.getInstance().getRecycleView();
            result = AccessibilityOperator.getInstance().clickListItem("android:id/list","书山宝",1);
            if(!result){
                return false;
            }else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            AccessibilityOperator.getInstance().getRecycleView();

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                result = AccessibilityOperator.getInstance().clickListSwitch("android:id/list","后台冻结",1,false);
                if(!result){
                    return false;
                }else {
                    AccessibilityOperator.getInstance().getRecycleView();
                    return AccessibilityOperator.getInstance().clickListSwitch("android:id/list","异常耗电自动优化",1,false);
                }
            }else {
                result = AccessibilityOperator.getInstance().clickListItemSwitch("android:id/list","后台冻结",1,false);
                if(!result){
                    return false;
                }else {
                    AccessibilityOperator.getInstance().getRecycleView();
                    return AccessibilityOperator.getInstance().clickListItemSwitch("android:id/list","检测到异常时自动优化",1,false);
                }
            }


        }
    }

    public boolean openComplete() {
        AccessibilityOperator.getInstance().getRecycleView();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        boolean result = false;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            result = AccessibilityOperator.getInstance().clickListItem("android:id/list","自启动管理",1);
            if(!result){
                return false;
            }else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                result = AccessibilityOperator.getInstance().clickListSwitch("android:id/list","书山宝",1,true);
            }
        }else {
            result = AccessibilityOperator.getInstance().clickListItemSwitch("android:id/list","书山宝",1,true);
        }

        return result;
    }

    public boolean openFloatManager() {
        AccessibilityOperator.getInstance().getRecycleView();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        boolean result = AccessibilityOperator.getInstance().clickListItemSwitch("android:id/list","书山宝",1,true);
        if(!result){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            result = AccessibilityOperator.getInstance().clickListSwitch("android:id/list","书山宝",1,true);
        }

        return result;
    }

    public boolean openUsageSetting() {
        AccessibilityOperator.getInstance().getRecycleView();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean result = AccessibilityOperator.getInstance().clickListItemSwitch("android:id/list","书山宝",1,true);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            AccessibilityOperator.getInstance().getRecycleView();
            return AccessibilityOperator.getInstance().clickByText("允许");

        }
    }

    public boolean enableAdmin() {
        AccessibilityOperator.getInstance().getRecycleView();
        boolean result = AccessibilityOperator.getInstance().clickById("com.android.settings:id/add_device");
        if(!result){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            result = AccessibilityOperator.getInstance().clickById("com.android.settings:id/action_button");
        }

        return result;
    }
}
